﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace CodeTalk.CTSpeechCommands
{
    public class TextCommand : SpeechCommand
    {
        public string WrittenForm { get; set; }
        public string SpokenForm { get; private set; }
        public string UndoText { get; set; }
        public bool AddPreSpace { get; set; }
        public bool RemPreSpace { get; set; }

        internal override GrammarBuilder GrammarBld(Context context, Context[] libraries)
        {
            return new GrammarBuilder(new SemanticResultKey("WrittenForm", new SemanticResultValue(SpokenForm, WrittenForm)));
            //return new SemanticResultValue(SpokenForm, SemanticKey + ";" + WrittenForm);
        }

        internal override string SemanticKey
        {
            get { return "TextCommand:" + SpokenForm; }
        }

        public override string Name
        {
            get { return "TextCommand"; }
        }

        public override string Description
        {
            get { return "TextCommand: " + SpokenForm; }
        }

        public override string GetAlternativesFeedback(string recognizedText, SemanticValue semValue)
        {
            return WrittenForm;
        }

        public TextCommand(string WrittenForm, string SpokenForm)
        {
            this.WrittenForm = WrittenForm;
            this.SpokenForm = SpokenForm;
            this.UndoText = "^z";
            this.AddPreSpace = false;
            this.RemPreSpace = false;
        }

        internal override bool CanSpokenFormMatch(string spokenForm)
        {
            return spokenForm == SpokenForm;
        }

        internal override object Execute(SpeechMode callingMode, string recognizedText, SemanticValue semValue)
        {
            if (RemPreSpace)
                callingMode.CurrentOutputHandler.RemoveLastChar(' ');

            callingMode.CurrentOutputHandler.SendKeys(WrittenForm);
            callingMode.CurrentGuiHandler.SetPrimaryFeedback(WrittenForm);
            return UndoText;
        }

        internal string Serialize()
        {
            return WrittenForm + "€" + SpokenForm + "€" + UndoText + "€" + AddPreSpace + "€" + RemPreSpace + "€" + Active;
        }

        internal static TextCommand Deserialize(string line)
        {
            try
            {
                string[] tokens = line.Split('€');
                if (tokens.Length != 6)
                    return null;
                TextCommand tc = new TextCommand(tokens[0], tokens[1]);
                tc.UndoText = tokens[2];
                tc.AddPreSpace = bool.Parse(tokens[3]);
                tc.RemPreSpace = bool.Parse(tokens[4]);
                tc.Active = bool.Parse(tokens[5]);
                return tc;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
